//
//  RanIntroduceAnimationPageFive.m
//  test
//
//  Created by Ran on 15/10/29.
//  Copyright (c) 2015年 gintong. All rights reserved.
//

#import "RanIntroduceAnimationPageFive.h"

@interface RanIntroduceAnimationPageFive()

@property(nonatomic, weak)UILabel *titleLabel;
@property(nonatomic, weak)UILabel*detailLabel;

@property(nonatomic, strong)UIImageView *houseImgView;
@property(nonatomic, strong)UIImageView *phoneImgView;
@property(nonatomic, strong)UIImageView *glassImgView;
@property(nonatomic, strong)UIImageView *paperImgView;
@property(nonatomic, strong)UIImageView *calculatorImgView;

@property(nonatomic, strong)UIButton *startBtn;
@property(nonatomic, copy)void(^response)(UIButton *btn);

@end

@implementation RanIntroduceAnimationPageFive

#pragma mark - System

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Private

- (void)setupUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    UIImageView *backgroudImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    [self addSubview:backgroudImageView];
    backgroudImageView.image = [UIImage imageNamed:@"KFC.bundle/5.bundle/bg-Portrait-568h"];
}

- (void)addAnimations
{
    CGFloat textAnimationDuration = 1;
    
    //类似的动画应该抽取一下
    [self addAnimationToTextLayer:self.titleLabel.layer withDuration:textAnimationDuration bottomSpacing:kPercent * 130.0];
    [self addAnimationToTextLayer:self.detailLabel.layer withDuration:textAnimationDuration bottomSpacing:kPercent * 90.0];

    
    [self.houseImgView.layer addAnimation:({
        
        CABasicAnimation *opacityAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAni.fromValue = @1;
        opacityAni.toValue = @1;
        
        CAKeyframeAnimation *positionAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        positionAni.values = @[
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth / 2, - self.houseImgView.frame.size.height / 2)],
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth / 2, 260.0 * kPercent)]
                               ];
        positionAni.keyTimes = @[@0, @0.7];
        positionAni.removedOnCompletion = NO;
        positionAni.fillMode = kCAFillModeForwards;
        
        CAKeyframeAnimation *rotationAni = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAni.values = @[@(0), @(M_PI * 0.1), @(- M_PI * 0.05), @(0)];
        rotationAni.keyTimes = @[@0, @0.7, @0.9, @1];
        
        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.removedOnCompletion = NO;
        group.fillMode = kCAFillModeForwards;
        group.duration = textAnimationDuration;
        group.animations = @[positionAni, rotationAni, opacityAni];
        
        group;
        
    }) forKey:nil];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.startBtn.alpha = 1;
    }];
    
    CGFloat itemAnimationDuration = 0.7;
    [self addAnimationToItemLayer:self.glassImgView.layer duration:itemAnimationDuration beginTime:textAnimationDuration endPosition:CGPointMake(54 * kPercent, 160 * kPercent)];
    [self addAnimationToItemLayer:self.phoneImgView.layer duration:itemAnimationDuration beginTime:textAnimationDuration endPosition:CGPointMake(263 * kPercent, 160 * kPercent)];
    [self addAnimationToItemLayer:self.paperImgView.layer duration:itemAnimationDuration beginTime:textAnimationDuration endPosition:CGPointMake(57 * kPercent, 319 * kPercent)];
    [self addAnimationToItemLayer:self.calculatorImgView.layer duration:itemAnimationDuration beginTime:textAnimationDuration endPosition:CGPointMake(264 * kPercent, 316 * kPercent)];
    
    
}

- (void)addAnimationToTextLayer: (CALayer *)layer withDuration: (CGFloat)duration bottomSpacing: (CGFloat)bottomSpacing
{
    [layer addAnimation:({
        
        CABasicAnimation *opacityAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAni.fromValue = @1;
        opacityAni.toValue = @1;
        
        CAKeyframeAnimation *positionAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        positionAni.values = @[
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth * 1.5, kScreenHeight - kPercent * bottomSpacing)],
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth / 2 * 0.8, kScreenHeight - kPercent * bottomSpacing)],
                               [NSValue valueWithCGPoint:CGPointMake(kScreenWidth / 2, kScreenHeight - kPercent * bottomSpacing)]
                               ];
        positionAni.keyTimes = @[@0, @0.8, @1];
        
        CAAnimationGroup *aniGroup = [CAAnimationGroup animation];
        aniGroup.animations = @[opacityAni, positionAni];
        aniGroup.duration = duration;
        aniGroup.removedOnCompletion = NO;
        aniGroup.fillMode = kCAFillModeForwards;
        
        aniGroup;
        
    }) forKey:nil];
}

- (void)addAnimationToItemLayer: (CALayer *)layer duration: (CGFloat)duration beginTime: (CGFloat)beginTime endPosition: (CGPoint)endPosition
{
    [layer addAnimation:({
    
        CABasicAnimation *opacityAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAni.fromValue = @1;
        opacityAni.toValue = @1;
        
        CABasicAnimation *positionAni = [CABasicAnimation animationWithKeyPath:@"position"];
        positionAni.fromValue = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth / 2, 260 * kPercent)];
        positionAni.toValue = [NSValue valueWithCGPoint:endPosition];
        
        CABasicAnimation *scaleAni = [CABasicAnimation animationWithKeyPath:@"transform"];
        scaleAni.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 1)];
        scaleAni.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
        
        CAAnimationGroup *aniGroup = [CAAnimationGroup animation];
        aniGroup.removedOnCompletion = NO;
        aniGroup.fillMode = kCAFillModeForwards;
        aniGroup.duration = duration;
        aniGroup.animations = @[opacityAni, positionAni, scaleAni];
        aniGroup.beginTime = CACurrentMediaTime() + beginTime;
        
        aniGroup;
        
    }) forKey:nil];
}

- (void)startBtnClicked: (UIButton *)btn
{
    if (self.startBtnClickedResponse) {
        self.startBtnClickedResponse(btn);
    }
}


#pragma mark - Protocol

- (void)showAnimations
{
    [self reset];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self addAnimations];
    });
}

- (void)reset
{
    [self.subviews enumerateObjectsUsingBlock:^(UIView *subView, NSUInteger idx, BOOL *stop) {
        [subView.layer removeAllAnimations];
    }];
    self.startBtn.alpha = 0;
}

- (void (^)(UIButton *))startBtnClickedResponse
{
    return self.response;
}

- (void)setStartBtnClickedResponse:(void (^)(UIButton *))startBtnClickedResponse
{
    self.response = startBtnClickedResponse;
}

#pragma mark - Getter

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40 * kPercent)];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:30 * kPercent];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"发现";
        label.layer.opacity = 0;
        [self addSubview:label];
        self.titleLabel = label;
    }
    return _titleLabel;
}

- (UILabel *)detailLabel
{
    if (!_detailLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20 * kPercent)];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:16 * kPercent];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"瞅瞅神马变化发生在KFC!";
        label.layer.opacity = 0;
        [self addSubview:label];
        self.detailLabel = label;
    }
    return _detailLabel;
}

- (UIImageView *)imageViewWithName: (NSString *)imageName
{
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: imageName]];
    imgView.layer.opacity = 0;
    [self addSubview:imgView];
    return imgView;
}

- (UIImageView *)houseImgView
{
    if (!_houseImgView) {
        self.houseImgView = ({
            UIImageView *imgView = [self imageViewWithName:@"KFC.bundle/5.bundle/1"];
            imgView.frame = CGRectMake(0, 0, 222 * kPercent, 177 * kPercent);
            imgView;
        });
    }
    return _houseImgView;
}

- (UIImageView *)phoneImgView
{
    if (!_phoneImgView) {
        self.phoneImgView = ({
            UIImageView *imgView = [self imageViewWithName:@"KFC.bundle/5.bundle/4"];
            imgView.frame = CGRectMake(0, 0, 104 * kPercent, 99 * kPercent);
            imgView;
        });
    }
    return _phoneImgView;
}

- (UIImageView *)glassImgView
{
    if (!_glassImgView) {
        self.glassImgView = ({
            UIImageView *imgView = [self imageViewWithName:@"KFC.bundle/5.bundle/2"];
            imgView.frame = CGRectMake(0, 0, 92 * kPercent, 90 * kPercent);
            imgView;
        });
    }
    return _glassImgView;
}

- (UIImageView *)paperImgView
{
    if (!_paperImgView) {
        self.paperImgView = ({
            UIImageView *imgView = [self imageViewWithName:@"KFC.bundle/5.bundle/3"];
            imgView.frame = CGRectMake(0, 0, 124 * kPercent, 90 * kPercent);
            imgView;
        });
    }
    return _paperImgView;
}

- (UIImageView *)calculatorImgView
{
    if (!_calculatorImgView) {
        self.calculatorImgView = ({
            UIImageView *imgView = [self imageViewWithName:@"KFC.bundle/5.bundle/5"];
            imgView.frame = CGRectMake(0, 0, 104 * kPercent, 90 * kPercent);
            imgView;
        });
    }
    return _calculatorImgView;
}

- (UIButton *)startBtn
{
    if (!_startBtn) {
        self.startBtn = ({
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(250 * kPercent, kScreenHeight - 60 * kPercent, 43, 43)];
            btn.alpha = 0;
            [btn setBackgroundImage:[UIImage imageNamed:@"KFC.bundle/5.bundle/6"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(startBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            btn;
        });
    }
    return _startBtn;
}

@end
